<?PHP
/*
  Plugin for download from megaupload (http://megaupload.com/)
  Developed by seth <xd dot seth at gmail dot com - http://elrincondeseth.wordpress.com> using Shaun Friedle's mu_autocaptcha <http://hg.herecomethelizards.co.uk/mu_autocaptcha/>
*/
global $muAutoCaptchaPath;
$muAutoCaptchaPath = resolveEnv($muAutoCaptchaPath);
if (!file_exists($muAutoCaptchaPath.'mu_autocaptcha.py')){
    echo 'Warning: MU Autocaptcha not found in '.$muAutoCaptchaPath."\n";
}
$plugins[] = array('megaupload','@(http://|http://www.)megaupload.com/\?d=.+@');

function megaupload_download($url, $extra)
{
    global $muAutoCaptchaPath;
    
    $file = downloadUrl($url);
    if(($file['code']<200) or ($file['code']>299)) {
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Download error. Response code #'.$file['code'],
        );
    }
    
    $file = $file['content'];
    
//    preg_match('@</font> <font style="font-family:arial; color:#FF6700; font-size:(22|18)px; font-weight:bold;">(.+?)</font><br>@',$file, $matchs);
//    $filePath = $matchs[1];
//    if(!$filePath) file_put_contents('/home/seth/lalalaqqq.txt',$file);
    
    preg_match('@captchacode" value="(.+?)">@',$file, $matchs);
    $postData = 'captchacode='.$matchs[1];

    preg_match('@name="megavar" value="(.+?)">@',$file, $matchs);
    $postData.= '&megavar='.$matchs[1];
    
    preg_match('@<TD width="100" align="center" height="40"><img src="(.*)" border="0" alt=""></TD>@',$file,$matchs);

    $captcha = downloadUrl($matchs[1]);
    file_put_contents('./tmp/'.basename($matchs[1]), $captcha['content']);
    $postData .= '&captcha='.rtrim(strtoupper(shell_exec('python '.$muAutoCaptchaPath.'mu_autocaptcha.py -n '.$muAutoCaptchaPath.'neuralnet.pkl -s '.$muAutoCaptchaPath.'sample/ '.escapeshellarg(realpath('./tmp/'.basename($matchs[1]))))));
    
    
    $file = downloadUrl($url,'POST',$postData);
    if(($file['code']<200) or ($file['code']>299)) {
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Download error. Response code #'.$file['code'],
        );
    }
    
    if(!preg_match('@id="downloadlink"><a href="(.*?)"@', $file['content'], $matchs)) {
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Failed solving the captcha',
        );
    }
    
    sleep(45);
    $file = downloadUrlRecursive($matchs[1]);

    if(($file['code']<200) or ($file['code']>299)) {        
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Download error. Response code #'.$file['code'],
                        'content'      => ''
        );
        
    }else{
        $filepath = basename($matchs[1]);
        return array(
                        'status'   => STATUS_DOWNLOADED,
                        'content'  => $file['content'],
                        'filePath' => $filepath
        );
    }
}


function megaupload_getName($url)
{
    parse_str(parse_url($url, PHP_URL_QUERY), $query);
    return $query['d'];

}


?>

